import math

PI=3.1415926

#Window sampling, specified as one of the following:

#'symmetric' — Use this option when using windows for filter design.

#'periodic' — This option is useful for spectral analysis because it enables a windowed signal 
#to have the perfect periodic extension implicit in the discrete Fourier transform. When 'periodic' is specified, 
#hann computes a window of length L + 1 and returns the first L points.
#

def parse_arg(L,sflag):
	if sflag=='symmetric':
	   N=L-1
	   win_len_plus_1=L
	elif sflag=='periodic':
	   N=L
	   win_len_plus_1=L
	else:
	   raise ValueError('unrecognized sflag:%s'%sflag)
#	print(win_len_plus_1,N)
	return win_len_plus_1,N
	
# hann design window
# w(n)=0.5(1-cos(2PI*n/N)),0<=n<=N
#window length L=N+1
#output : column vector
def hann(L,sflag='symmetric'):
	w=[]

	win_len_plus_1,N=parse_arg(L,sflag)
	for n in range(win_len_plus_1):
	   v = 0.5*(1-math.cos(2*PI*n/N))
	   w.append(v)
   
	return w

# a hamming window has the following form: 
# w(n)=0.541-0.46cos(2PI*n/N-1)),0<=n<=N
# N is the window length
def hamming(L,sflag='symmetric'):
	w=[]
	win_len_plus_1,N=parse_arg(L,sflag)
	for n in range(win_len_plus_1):
	   v = 0.54-0.46*math.cos(2*PI*n/N)
	   w.append(v)
	return w
	
  
if __name__ == "__main__":
   w = hann(64)
   for v in w:
     print('%7.4f'%(v),end=',')
   print()	 
   w = hann(64,'periodic')
   for v in w:
     print('%7.4f'%(v),end=',')	 
   print()	 	 
   w = hamming(64)
   for v in w:
     print('%7.4f'%(v),end=',')
   print()	 
   w = hamming(64,'periodic')
   for v in w:
     print('%7.4f'%(v),end=',')	 	 